/**
 *   Copyright (c) 2011 Kenth Fagerlund   
 *
 *   This file is part of Sensory Commander.
 *
 *   Sensory Commander is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   Sensory Commander is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with Sensory Commander.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <sys/stat.h>
#include <syslog.h>
#include "socketsrv.h"

#define APP_CMD_NAME     "sensecmd"
#define APP_FULL_NAME    "Sensory Commander"
#define APP_VERSION      "0.1 Alpha"

#define DEFAULT_PORT     20762
#define MIN_PORT_VALUE   1024
#define MAX_PORT_VALUE   65535

enum LAUNCH_ERROR
  {
    LAUNCH_ERROR_PORT_OUT_OF_RANGE
  };

//------------ Prototypes -------------//
void faillaunch(enum LAUNCH_ERROR error);
void printhelpmsg();
void startprocess();

//------------ Main ------------//
int main(int argc, char *argv[])
{

  unsigned int portNumber = DEFAULT_PORT; //Default port

  /*
   * Lets start by dealing with the command line arguments
   */
  if(argc > 0)
    {
      //Found more then one. Let's go throu them all.
      for(int i = 0; i < argc; i++)
	{
	  //Is it a --help argument?
	  if (strcmp(argv[i], "--help") == 0 || strcmp(argv[i], "-h") == 0)
	    {
	      //It is! Print the help text for the poor guy
	      printhelpmsg();
	    }
	  if(strstr(argv[i], "--port") != 0)
	    {
	      i++;
	      portNumber = atoi(argv[i]);
	      if(portNumber < MIN_PORT_VALUE || portNumber > MAX_PORT_VALUE)
		{
		  faillaunch(LAUNCH_ERROR_PORT_OUT_OF_RANGE);
		}
	    }
	}
    }  
  //Seems like we are good to go.
  startprocess();
}

void startprocess()
{
  pid_t pid,sid;

  pid = fork();

  if(pid < 0) {
    exit(EXIT_FAILURE);
  }else if (pid > 0) {
    exit(EXIT_SUCCESS);
  }
  openlog(APP_FULL_NAME,0,0);
  umask(0);
  
  sid = setsid();
  if(sid < 0) {
    syslog(LOG_EMERG,"Failed to set SID number");
    exit(EXIT_FAILURE);
  }
  
  if((chdir("/")) < 0) {
    syslog(LOG_EMERG,"Failed to change to \"/\" directory");
    exit(EXIT_FAILURE);
  }

  close(STDIN_FILENO);
  close(STDOUT_FILENO);
  close(STDERR_FILENO);
  
  if(setupsocket() < 0) {
    syslog(LOG_EMERG,"Failed to setup the socke server");
    exit(EXIT_FAILURE);
  }

  while(1){
    
  }


  exit(EXIT_SUCCESS);
          
}

void faillaunch(enum LAUNCH_ERROR error)
{
  switch(error){
  case LAUNCH_ERROR_PORT_OUT_OF_RANGE:
    printf("ERROR: Port number is out of range.\n\n");
    break;
  }
  printhelpmsg();
  exit(EXIT_FAILURE);
}

void printhelpmsg()
{
  printf("%s - v%s\n", APP_FULL_NAME, APP_VERSION);
  printf(" \n");
  printf("Usage: %s [options...]\n", APP_CMD_NAME);
  printf("Options:\n");
  printf("  --help,-h             Help menu (The thing your are looking at)\n");
  printf("  --port=[port number]  Set port number for the server\n");
  printf("  --debug-level=[Level] Set to debug mode. Levels are: Low/Medium/High. This reflect on how much debug text it will dump out.\n");
  printf("                          i.e High = High amount of debug output\n");
  printf("  --config=[Path]       Sets the path to the config file. Default path is ~/sensecmd.config\n");
}
